<template>
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>

    <div style="margin-left: 10px;">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="物料编码" prop="goodsCode">
            <el-input v-model="queryParams.goodsCode" placeholder="请输入物料编码"
                      clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item label="物料名称" prop="goodsName">
            <el-input v-model="queryParams.goodsName" placeholder="请输入物料名称"
                      clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item label="物料型号" prop="goodModel">
            <el-input v-model="queryParams.goodModel" placeholder="请输入物料型号"
                      clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <!--<el-form-item label="型号明细" prop="goodsBrand">
            <el-input v-model="queryParams.goodsBrand" placeholder="请输入物料型号明细"
                      clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddByGoods()" v-if="gsaleId"
                     >选择物料（产品）</el-button>

            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddByIron()" v-if="ironDetailId"
                     >选择物料(铁件)</el-button>
          </el-form-item>
        </el-form>
        <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table" :max-height="tableMaxHeight" @sort-change="handleSortChange" show-summary :summary-method="getSummaries">
          <el-table-column sortable label="物料编号" align="center" prop="goodsCode"/>
          <el-table-column sortable label="物料名称" align="center" prop="goodsName"/>
          <!--<el-table-column label="物料类型" align="center" prop="goodsType"/>-->
         <!-- <el-table-column label="物料型号明细" align="center" prop="goodsBrand"/>-->
          <el-table-column sortable label="物料型号" align="center" prop="goodModel"/>
          <el-table-column sortable label="计量单位" align="center" prop="goodsUnit"/>
          <el-table-column label="物料数量" align="center" prop="goodsCount"/>
          <el-table-column label="物料单价" align="center" prop="goodsBuyPrice"/>
          <el-table-column label="物料总价" align="center" prop="goodsBuyTotal"/>
         <!-- <el-table-column label="供应商" align="center" prop="supplierId"/>
          <el-table-column label="供应商名称" align="center" prop="supplierName"/>
          <el-table-column label="销售人姓名" align="center" prop="saleName"/>
          <el-table-column label="备注" align="center" prop="remark"/>
          <el-table-column label="状态" align="center" prop="status"/>-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-document"
                         @click="lingliaoMingxi(scope.row)" v-hasPermi="['develop:GoodsSaleMaterial:edit']">领料明细</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit"
                         @click="handleUpdate(scope.row)" v-hasPermi="['develop:GoodsSaleMaterial:edit']">修改数量</el-button>
              <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                             style="margin-left: 10px;">
                <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                           v-hasPermi="['develop:GoodsSaleMaterial:remove']">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      <GoodsSaleMaterialCount-form ref="form" @ok="getList"></GoodsSaleMaterialCount-form>
      <goodsOrderInForm-list ref="goodsOrderinform" @ok="getList"></goodsOrderInForm-list>
      <lingliaoMingxi-form-list ref="lingliaoMingxiForm" @ok="getList"></lingliaoMingxi-form-list>
    </div>
    <div slot="footer" class="dialog-footer">
     <!-- <el-button type="primary"  @click="submitGxGyGoods">确 认</el-button>-->
      <el-button @click="cancel">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getAction,addAction,deleteAction,accAdd} from '@/api/manage'
  import GoodsOrderInFormList from '@/views/develop/projectBaseInfo/prjForGoodsIn/GoodsOrderInFormList'
  import LingliaoMingxiFormList from '@/views/develop/goodsSaleMaterial/module/LingliaoMingxiFormList'
  import GoodsSaleMaterialCountForm from '@/views/develop/projectBaseInfo/prjForGoodsIn/GoodsSaleMaterialCountForm'

  export default {
    name: "ChajieForm",
    components: {
      GoodsOrderInFormList,
      LingliaoMingxiFormList,
      GoodsSaleMaterialCountForm
    },
    data() {
      return {
        title: "1产品物料列表",
        open: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        rows: [],
        rules: {
        },
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 销售信息管理表格数据
        dataList: [],
        columns: [],
        goodsId: null,
        gsaleCount: null,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20000,
          goodsCode: null,
          goodsName: null,
          goodModel: null,
          goodsBrand: null,
          gsaleId: null,
          code: null,
          name: null,
          params: {},
        },
        url: {
          list:'/goodsSaleMaterial/goodsSaleMaterial/list',
          remove:'/goodsSaleMaterial/goodsSaleMaterial/haveGoood/',
        },
        tableMaxHeight: window.innerHeight - 320,
        gsaleId: null,
        ironDetailId: null,
      };
    },
    created() {
      // this.intDictsForPrj();
      window.addEventListener('resize', this.getTableHeight);
    },
    destroyed() {
      window.removeEventListener('resize', this.getTableHeight);
    },
    methods: {
      getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if ( column.property === "goodsCount" || column.property === "goodsBuyTotal" || column.property === "goodsBuyPrice") {
          sums[index] = values.reduce((prev, curr) => {

            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev , curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
      lingliaoMingxi(row){
        this.$refs.lingliaoMingxiForm.show(row);
      },
       /** 排序触发事件 */
     handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleAddByGoods() {
        this.$refs.goodsOrderinform.showOrderInByGoods(this.gsaleId,this.gsaleCount);
      },

      handleAddByIron() {
        this.$refs.goodsOrderinform.showOrderInByIron(this.ironDetailId,this.gsaleCount);
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$refs.form.handleUpdate(row);
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        deleteAction(this.url.remove, ids).then(response => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      getTableHeight() {
        this.tableHeight = window.innerHeight - 320
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      chaijieGoods(row){
        this.queryParams.gsaleId=row.id;
        this.gsaleId=row.id;
        this.gsaleCount=row.count;
        this.open=true;
        this.getList();
      },

      bomByIron(row){
        this.queryParams.ironDetailId=row.id;
        this.ironDetailId=row.id;
        this.open=true;
        this.getList();
      },

      /** 查询销售信息管理列表 */
      getList() {
        this.loading = true;
        getAction(this.url.list, this.queryParams).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
    }
  };
</script>
